package 蚂蚁课堂;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;


public class FutureDemo {

    static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

     String str="";

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureDemo futureDemo=new FutureDemo();
        futureDemo.testFuture();
    }
    public void testFuture() throws ExecutionException, InterruptedException {
        Future<String> f1 = cachedThreadPool.submit(() -> {
            TimeUnit.SECONDS.sleep(1);
            return "吴永杰下单了";
        });

        
        String s = f1.get();
        str+=s;
        System.out.println(s);


        Future<String> f2 = cachedThreadPool.submit(() -> {
            System.out.println("====去超市买厨具============");
            TimeUnit.SECONDS.sleep(5);
            return str+="---->去超市买厨具【锅、勺子、铲子 都已经买回来了】";
        });


        Future<String> f3 = cachedThreadPool.submit(() -> {
            System.out.println("======去超市购买食材============");
            TimeUnit.SECONDS.sleep(5);
            return str+="---->去超市购买食材【油、盐、酱、醋 都已经买回来】";
        });

        /**
         * 第四步开始烧菜前提：厨具已经买回来、食材已经买回来，否则无法烧菜
         */
        //1：问厨具买回来没
        if (!f2.isDone()){
            System.out.println("厨具没买回来，等待3秒");
            TimeUnit.SECONDS.sleep(10);
        }

        if (!f3.isDone()){
            System.out.println("石材没买回来，等待3秒");
            TimeUnit.SECONDS.sleep(10);
        }

        Future<String> f4 = cachedThreadPool.submit(() -> {
            System.out.println("开始烧菜");
            return str+="---->开始烧菜";
        });

        f4.get();
        System.out.println(">>>>>>>>>>>>>>>>str"+str);

    }
}
